package com.serviceImpl;

import com.qzw.common.data.BaseMysqlDao;
import com.qzw.common.service.BaseServiceImpl;
import com.model.User;
import org.springframework.stereotype.Service;
import com.dao.UserDao;
import com.entity.GoUser;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
import java.lang.String;
import java.lang.Integer;
import com.query.UserQuery;
import com.service.IUserService;

@Service
public class UserServiceImpl extends BaseServiceImpl<User, GoUser, Integer, UserQuery> implements IUserService {

	@Autowired
	private UserDao dao;

	@Override protected BaseMysqlDao getDao() {
	return dao;
	}

	@Override
	public User entity2model(GoUser entity) {
		User model = null;
		if(isNotEmpty(entity)){
			model = new User();
			model.setId(entity.getId());
			model.setUsername(entity.getUsername());
			model.setPassword(entity.getPassword());
		}
		return model;
	}

	@Override
	public GoUser model2Entity(User model) {
		GoUser entity = null;
		if(isNotEmpty(model)){
			entity = new GoUser();
			entity.setId(model.getId());
			entity.setUsername(model.getUsername());
			entity.setPassword(model.getPassword());
		}
		return entity;
	}

	@Override
	public User update(User model) {
		if(model.isNew()){
			model = super.add(model);
		} else {
			Optional<GoUser> entityOptional = dao.findById(model.getId());GoUser entity = entityOptional.get();
			if(isNotEmpty(model.getUsername()))
				entity.setUsername(model.getUsername());
			if(isNotEmpty(model.getPassword()))
				entity.setPassword(model.getPassword());
			model = entity2model(dao.save(entity));
		}
		return model;
	}


}